##===----------------------------------------------------------------------===##
##
## This source file is part of the SwiftCrypto open source project
##
## Copyright (c) 2021 Apple Inc. and the SwiftCrypto project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.txt for the list of SwiftCrypto project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##

add_library(CCryptoBoringSSL STATIC
  "crypto/aes/aes.cc"
  "crypto/asn1/a_bitstr.cc"
  "crypto/asn1/a_bool.cc"
  "crypto/asn1/a_d2i_fp.cc"
  "crypto/asn1/a_dup.cc"
  "crypto/asn1/a_gentm.cc"
  "crypto/asn1/a_i2d_fp.cc"
  "crypto/asn1/a_int.cc"
  "crypto/asn1/a_mbstr.cc"
  "crypto/asn1/a_object.cc"
  "crypto/asn1/a_octet.cc"
  "crypto/asn1/a_strex.cc"
  "crypto/asn1/a_strnid.cc"
  "crypto/asn1/a_time.cc"
  "crypto/asn1/a_type.cc"
  "crypto/asn1/a_utctm.cc"
  "crypto/asn1/asn1_lib.cc"
  "crypto/asn1/asn1_par.cc"
  "crypto/asn1/asn_pack.cc"
  "crypto/asn1/f_int.cc"
  "crypto/asn1/f_string.cc"
  "crypto/asn1/posix_time.cc"
  "crypto/asn1/tasn_dec.cc"
  "crypto/asn1/tasn_enc.cc"
  "crypto/asn1/tasn_fre.cc"
  "crypto/asn1/tasn_new.cc"
  "crypto/asn1/tasn_typ.cc"
  "crypto/asn1/tasn_utl.cc"
  "crypto/base64/base64.cc"
  "crypto/bio/bio.cc"
  "crypto/bio/bio_mem.cc"
  "crypto/bio/connect.cc"
  "crypto/bio/errno.cc"
  "crypto/bio/fd.cc"
  "crypto/bio/file.cc"
  "crypto/bio/hexdump.cc"
  "crypto/bio/pair.cc"
  "crypto/bio/printf.cc"
  "crypto/bio/socket.cc"
  "crypto/bio/socket_helper.cc"
  "crypto/blake2/blake2.cc"
  "crypto/bn/bn_asn1.cc"
  "crypto/bn/convert.cc"
  "crypto/bn/div.cc"
  "crypto/bn/exponentiation.cc"
  "crypto/bn/sqrt.cc"
  "crypto/buf/buf.cc"
  "crypto/bytestring/asn1_compat.cc"
  "crypto/bytestring/ber.cc"
  "crypto/bytestring/cbb.cc"
  "crypto/bytestring/cbs.cc"
  "crypto/bytestring/unicode.cc"
  "crypto/chacha/chacha.cc"
  "crypto/cipher/derive_key.cc"
  "crypto/cipher/e_aesctrhmac.cc"
  "crypto/cipher/e_aeseax.cc"
  "crypto/cipher/e_aesgcmsiv.cc"
  "crypto/cipher/e_chacha20poly1305.cc"
  "crypto/cipher/e_des.cc"
  "crypto/cipher/e_null.cc"
  "crypto/cipher/e_rc2.cc"
  "crypto/cipher/e_rc4.cc"
  "crypto/cipher/e_tls.cc"
  "crypto/cipher/get_cipher.cc"
  "crypto/cipher/tls_cbc.cc"
  "crypto/cms/cms.cc"
  "crypto/conf/conf.cc"
  "crypto/cpu_aarch64_apple.cc"
  "crypto/cpu_aarch64_fuchsia.cc"
  "crypto/cpu_aarch64_linux.cc"
  "crypto/cpu_aarch64_openbsd.cc"
  "crypto/cpu_aarch64_sysreg.cc"
  "crypto/cpu_aarch64_win.cc"
  "crypto/cpu_arm_freebsd.cc"
  "crypto/cpu_arm_linux.cc"
  "crypto/cpu_intel.cc"
  "crypto/crypto.cc"
  "crypto/curve25519/curve25519.cc"
  "crypto/curve25519/curve25519_64_adx.cc"
  "crypto/curve25519/spake25519.cc"
  "crypto/des/des.cc"
  "crypto/dh/dh_asn1.cc"
  "crypto/dh/params.cc"
  "crypto/digest/digest_extra.cc"
  "crypto/dsa/dsa.cc"
  "crypto/dsa/dsa_asn1.cc"
  "crypto/ec/ec_asn1.cc"
  "crypto/ec/ec_derive.cc"
  "crypto/ec/hash_to_curve.cc"
  "crypto/ecdh/ecdh.cc"
  "crypto/ecdsa/ecdsa_asn1.cc"
  "crypto/ecdsa/ecdsa_p1363.cc"
  "crypto/engine/engine.cc"
  "crypto/err/err.cc"
  "crypto/evp/evp.cc"
  "crypto/evp/evp_asn1.cc"
  "crypto/evp/evp_ctx.cc"
  "crypto/evp/p_dh.cc"
  "crypto/evp/p_dh_asn1.cc"
  "crypto/evp/p_dsa_asn1.cc"
  "crypto/evp/p_ec.cc"
  "crypto/evp/p_ec_asn1.cc"
  "crypto/evp/p_ed25519.cc"
  "crypto/evp/p_ed25519_asn1.cc"
  "crypto/evp/p_hkdf.cc"
  "crypto/evp/p_rsa.cc"
  "crypto/evp/p_rsa_asn1.cc"
  "crypto/evp/p_x25519.cc"
  "crypto/evp/p_x25519_asn1.cc"
  "crypto/evp/pbkdf.cc"
  "crypto/evp/print.cc"
  "crypto/evp/scrypt.cc"
  "crypto/evp/sign.cc"
  "crypto/ex_data.cc"
  "crypto/fipsmodule/bcm.cc"
  "crypto/fipsmodule/fips_shared_support.cc"
  "crypto/fuzzer_mode.cc"
  "crypto/hpke/hpke.cc"
  "crypto/hrss/hrss.cc"
  "crypto/kyber/kyber.cc"
  "crypto/lhash/lhash.cc"
  "crypto/md4/md4.cc"
  "crypto/md5/md5.cc"
  "crypto/mem.cc"
  "crypto/mldsa/mldsa.cc"
  "crypto/mlkem/mlkem.cc"
  "crypto/obj/obj.cc"
  "crypto/obj/obj_xref.cc"
  "crypto/pem/pem_all.cc"
  "crypto/pem/pem_info.cc"
  "crypto/pem/pem_lib.cc"
  "crypto/pem/pem_oth.cc"
  "crypto/pem/pem_pk8.cc"
  "crypto/pem/pem_pkey.cc"
  "crypto/pem/pem_x509.cc"
  "crypto/pem/pem_xaux.cc"
  "crypto/pkcs7/pkcs7.cc"
  "crypto/pkcs7/pkcs7_x509.cc"
  "crypto/pkcs8/p5_pbev2.cc"
  "crypto/pkcs8/pkcs8.cc"
  "crypto/pkcs8/pkcs8_x509.cc"
  "crypto/poly1305/poly1305.cc"
  "crypto/poly1305/poly1305_arm.cc"
  "crypto/poly1305/poly1305_vec.cc"
  "crypto/pool/pool.cc"
  "crypto/rand/deterministic.cc"
  "crypto/rand/fork_detect.cc"
  "crypto/rand/forkunsafe.cc"
  "crypto/rand/getentropy.cc"
  "crypto/rand/ios.cc"
  "crypto/rand/passive.cc"
  "crypto/rand/rand.cc"
  "crypto/rand/trusty.cc"
  "crypto/rand/urandom.cc"
  "crypto/rand/windows.cc"
  "crypto/rc4/rc4.cc"
  "crypto/refcount.cc"
  "crypto/rsa/rsa_asn1.cc"
  "crypto/rsa/rsa_crypt.cc"
  "crypto/rsa/rsa_extra.cc"
  "crypto/rsa/rsa_print.cc"
  "crypto/sha/sha1.cc"
  "crypto/sha/sha256.cc"
  "crypto/sha/sha512.cc"
  "crypto/siphash/siphash.cc"
  "crypto/slhdsa/slhdsa.cc"
  "crypto/spake2plus/spake2plus.cc"
  "crypto/stack/stack.cc"
  "crypto/thread.cc"
  "crypto/thread_none.cc"
  "crypto/thread_pthread.cc"
  "crypto/thread_win.cc"
  "crypto/trust_token/pmbtoken.cc"
  "crypto/trust_token/trust_token.cc"
  "crypto/trust_token/voprf.cc"
  "crypto/x509/a_digest.cc"
  "crypto/x509/a_sign.cc"
  "crypto/x509/a_verify.cc"
  "crypto/x509/algorithm.cc"
  "crypto/x509/asn1_gen.cc"
  "crypto/x509/by_dir.cc"
  "crypto/x509/by_file.cc"
  "crypto/x509/i2d_pr.cc"
  "crypto/x509/name_print.cc"
  "crypto/x509/policy.cc"
  "crypto/x509/rsa_pss.cc"
  "crypto/x509/t_crl.cc"
  "crypto/x509/t_req.cc"
  "crypto/x509/t_x509.cc"
  "crypto/x509/t_x509a.cc"
  "crypto/x509/v3_akey.cc"
  "crypto/x509/v3_akeya.cc"
  "crypto/x509/v3_alt.cc"
  "crypto/x509/v3_bcons.cc"
  "crypto/x509/v3_bitst.cc"
  "crypto/x509/v3_conf.cc"
  "crypto/x509/v3_cpols.cc"
  "crypto/x509/v3_crld.cc"
  "crypto/x509/v3_enum.cc"
  "crypto/x509/v3_extku.cc"
  "crypto/x509/v3_genn.cc"
  "crypto/x509/v3_ia5.cc"
  "crypto/x509/v3_info.cc"
  "crypto/x509/v3_int.cc"
  "crypto/x509/v3_lib.cc"
  "crypto/x509/v3_ncons.cc"
  "crypto/x509/v3_ocsp.cc"
  "crypto/x509/v3_pcons.cc"
  "crypto/x509/v3_pmaps.cc"
  "crypto/x509/v3_prn.cc"
  "crypto/x509/v3_purp.cc"
  "crypto/x509/v3_skey.cc"
  "crypto/x509/v3_utl.cc"
  "crypto/x509/x509.cc"
  "crypto/x509/x509_att.cc"
  "crypto/x509/x509_cmp.cc"
  "crypto/x509/x509_d2.cc"
  "crypto/x509/x509_def.cc"
  "crypto/x509/x509_ext.cc"
  "crypto/x509/x509_lu.cc"
  "crypto/x509/x509_obj.cc"
  "crypto/x509/x509_req.cc"
  "crypto/x509/x509_set.cc"
  "crypto/x509/x509_trs.cc"
  "crypto/x509/x509_txt.cc"
  "crypto/x509/x509_v3.cc"
  "crypto/x509/x509_vfy.cc"
  "crypto/x509/x509_vpm.cc"
  "crypto/x509/x509cset.cc"
  "crypto/x509/x509name.cc"
  "crypto/x509/x509rset.cc"
  "crypto/x509/x509spki.cc"
  "crypto/x509/x_algor.cc"
  "crypto/x509/x_all.cc"
  "crypto/x509/x_attrib.cc"
  "crypto/x509/x_crl.cc"
  "crypto/x509/x_exten.cc"
  "crypto/x509/x_name.cc"
  "crypto/x509/x_pubkey.cc"
  "crypto/x509/x_req.cc"
  "crypto/x509/x_sig.cc"
  "crypto/x509/x_spki.cc"
  "crypto/x509/x_x509.cc"
  "crypto/x509/x_x509a.cc"
  "crypto/xwing/xwing.cc"
  "gen/crypto/err_data.cc"
  "$<$<NOT:$<PLATFORM_ID:Windows>>:crypto/hrss/asm/poly_rq_mul.S>"
  "$<$<NOT:$<PLATFORM_ID:Windows>>:third_party/fiat/asm/fiat_curve25519_adx_mul.S>"
  "$<$<NOT:$<PLATFORM_ID:Windows>>:third_party/fiat/asm/fiat_curve25519_adx_square.S>"
  "$<$<NOT:$<PLATFORM_ID:Windows>>:third_party/fiat/asm/fiat_p256_adx_mul.S>"
  "$<$<NOT:$<PLATFORM_ID:Windows>>:third_party/fiat/asm/fiat_p256_adx_sqr.S>")

if(CMAKE_SYSTEM_NAME STREQUAL Darwin AND CMAKE_SYSTEM_PROCESSOR MATCHES "amd64|x86_64")
  target_sources(CCryptoBoringSSL PRIVATE
    gen/bcm/aes-gcm-avx2-x86_64-apple.S
    gen/bcm/aes-gcm-avx512-x86_64-apple.S
    gen/bcm/aesni-gcm-x86_64-apple.S
    gen/bcm/aesni-x86_64-apple.S
    gen/bcm/ghash-ssse3-x86_64-apple.S
    gen/bcm/ghash-x86_64-apple.S
    gen/bcm/p256-x86_64-asm-apple.S
    gen/bcm/p256_beeu-x86_64-asm-apple.S
    gen/bcm/rdrand-x86_64-apple.S
    gen/bcm/rsaz-avx2-apple.S
    gen/bcm/sha1-x86_64-apple.S
    gen/bcm/sha256-x86_64-apple.S
    gen/bcm/sha512-x86_64-apple.S
    gen/bcm/vpaes-x86_64-apple.S
    gen/bcm/x86_64-mont-apple.S
    gen/bcm/x86_64-mont5-apple.S
    gen/crypto/aes128gcmsiv-x86_64-apple.S
    gen/crypto/chacha-x86_64-apple.S
    gen/crypto/chacha20_poly1305_x86_64-apple.S
    gen/crypto/md5-x86_64-apple.S)
elseif(CMAKE_SYSTEM_NAME MATCHES "Linux|Android|FreeBSD|OpenBSD" AND CMAKE_SYSTEM_PROCESSOR MATCHES "amd64|x86_64")
  target_sources(CCryptoBoringSSL PRIVATE
    gen/bcm/aes-gcm-avx2-x86_64-linux.S
    gen/bcm/aes-gcm-avx512-x86_64-linux.S
    gen/bcm/aesni-gcm-x86_64-linux.S
    gen/bcm/aesni-x86_64-linux.S
    gen/bcm/ghash-ssse3-x86_64-linux.S
    gen/bcm/ghash-x86_64-linux.S
    gen/bcm/p256-x86_64-asm-linux.S
    gen/bcm/p256_beeu-x86_64-asm-linux.S
    gen/bcm/rdrand-x86_64-linux.S
    gen/bcm/rsaz-avx2-linux.S
    gen/bcm/sha1-x86_64-linux.S
    gen/bcm/sha256-x86_64-linux.S
    gen/bcm/sha512-x86_64-linux.S
    gen/bcm/vpaes-x86_64-linux.S
    gen/bcm/x86_64-mont-linux.S
    gen/bcm/x86_64-mont5-linux.S
    gen/crypto/aes128gcmsiv-x86_64-linux.S
    gen/crypto/chacha-x86_64-linux.S
    gen/crypto/chacha20_poly1305_x86_64-linux.S
    gen/crypto/md5-x86_64-linux.S)
elseif(CMAKE_SYSTEM_NAME STREQUAL Darwin AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm64|aarch64")
  target_sources(CCryptoBoringSSL PRIVATE
    gen/bcm/aesv8-armv8-apple.S
    gen/bcm/aesv8-gcm-armv8-apple.S
    gen/bcm/armv8-mont-apple.S
    gen/bcm/bn-armv8-apple.S
    gen/bcm/ghash-neon-armv8-apple.S
    gen/bcm/ghashv8-armv8-apple.S
    gen/bcm/p256-armv8-asm-apple.S
    gen/bcm/p256_beeu-armv8-asm-apple.S
    gen/bcm/sha1-armv8-apple.S
    gen/bcm/sha256-armv8-apple.S
    gen/bcm/sha512-armv8-apple.S
    gen/bcm/vpaes-armv8-apple.S
    gen/crypto/chacha-armv8-apple.S
    gen/crypto/chacha20_poly1305_armv8-apple.S)
elseif(CMAKE_SYSTEM_NAME MATCHES "Linux|Android|FreeBSD|OpenBSD" AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm64|aarch64")
  target_sources(CCryptoBoringSSL PRIVATE
    gen/bcm/aesv8-armv8-linux.S
    gen/bcm/aesv8-gcm-armv8-linux.S
    gen/bcm/armv8-mont-linux.S
    gen/bcm/bn-armv8-linux.S
    gen/bcm/ghash-neon-armv8-linux.S
    gen/bcm/ghashv8-armv8-linux.S
    gen/bcm/p256-armv8-asm-linux.S
    gen/bcm/p256_beeu-armv8-asm-linux.S
    gen/bcm/sha1-armv8-linux.S
    gen/bcm/sha256-armv8-linux.S
    gen/bcm/sha512-armv8-linux.S
    gen/bcm/vpaes-armv8-linux.S
    gen/crypto/chacha-armv8-linux.S
    gen/crypto/chacha20_poly1305_armv8-linux.S)
elseif(CMAKE_SYSTEM_NAME MATCHES "Windows" AND CMAKE_SYSTEM_PROCESSOR MATCHES "AMD64|amd64|x86_64")
  target_sources(CCryptoBoringSSL PRIVATE
)
elseif(CMAKE_SYSTEM_NAME MATCHES "Windows" AND CMAKE_SYSTEM_PROCESSOR MATCHES "ARM64|arm64|aarch64")
  target_sources(CCryptoBoringSSL PRIVATE
    gen/bcm/aesv8-armv8-win.S
    gen/bcm/aesv8-gcm-armv8-win.S
    gen/bcm/armv8-mont-win.S
    gen/bcm/bn-armv8-win.S
    gen/bcm/ghash-neon-armv8-win.S
    gen/bcm/ghashv8-armv8-win.S
    gen/bcm/p256-armv8-asm-win.S
    gen/bcm/p256_beeu-armv8-asm-win.S
    gen/bcm/sha1-armv8-win.S
    gen/bcm/sha256-armv8-win.S
    gen/bcm/sha512-armv8-win.S
    gen/bcm/vpaes-armv8-win.S
    gen/crypto/chacha-armv8-win.S
    gen/crypto/chacha20_poly1305_armv8-win.S)
else()
   message(FATAL_ERROR "platform sources are not defined here for ${CMAKE_SYSTEM_NAME} on ${CMAKE_SYSTEM_PROCESSOR}")
endif()

target_include_directories(CCryptoBoringSSL PUBLIC
  include)

target_compile_definitions(CCryptoBoringSSL PRIVATE
  $<$<PLATFORM_ID:Windows>:WIN32_LEAN_AND_MEAN>)
target_link_libraries(CCryptoBoringSSL PUBLIC
  $<$<NOT:$<PLATFORM_ID:Darwin>>:dispatch>
  $<$<NOT:$<PLATFORM_ID:Darwin>>:Foundation>
  SwiftASN1)
set_target_properties(CCryptoBoringSSL PROPERTIES
  INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/include;${CMAKE_Swift_MODULE_DIRECTORY}")

set_property(GLOBAL APPEND PROPERTY SWIFT_CRYPTO_EXPORTS CCryptoBoringSSL)
